define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coll/log/index' + location.search,
                    // add_url: 'coll/log/add',
                    // edit_url: 'coll/log/edit',
                    // del_url: 'coll/log/del',
                    multi_url: 'coll/log/multi',
                    table: 'analysis_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.status == 2) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'log_id'},
                        {field: 'pay.name', title: __('支付方式')},
                        {field: 'handler', title: __('经办人')},
                        {field: 'settle_name', title: __('结算单位'),operate:'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'analysis_id', title: __('Analysis_id')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        {field: 'remark', title: __('Remark'),operate:false},
                        {field: 'is_income', title: __('is_income'), searchList: {1:__('收入'),2:__('支出')}},
                        {field: 'pay_time', title: __('过账时间'), operate:'RANGE', addclass:'datetimerange',formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'storehouse.name', title: __('所属仓库'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                if(row['status'] == 2){
                                    // this.parents('tr').attr('style','backgroun:red')
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){

                // if($(this).parents('tr').find('.btn-editone').length >0){
                //     return  false;
                // }

                var ids = $(this).parents('tr').find('.log_id').html()
                var url = Config.this_url+'/coll/log/detail?ids='+ids
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "记录详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/coll/log/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            $('.joint-search').click(function () {
                console.log()
                console.log()
                    var slip_id = "{$row.sales_slip_id}"
                    var turl = Config.this_url+'/'+$(this).data('action')+'/detail?ids='+$(this).data('id')
                    Backend.api.addtabs(turl, $(this).html(), 'fa fa-circle-o fa-fw');
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});